package com.mooc.ppjoke.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.paging.PagedList;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mooc.lib_common.EmptyView;
import com.mooc.ppjoke.R;
import com.mooc.ppjoke.databinding.LayoutRefreshViewBinding;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @ClassName: AblistFragment$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/18 08:43
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public abstract class AbListFragment<T, M extends AbsViewModel<T>>  extends Fragment implements OnRefreshLoadMoreListener {

    protected RecyclerView rv;
    protected SmartRefreshLayout refreshLayout;
    protected LayoutRefreshViewBinding bind;
    protected EmptyView empty;
    protected PagedListAdapter<T, RecyclerView.ViewHolder> adapter;
    protected M mViewModel;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        bind = LayoutRefreshViewBinding.inflate(inflater, container, false);
        rv = bind.recyclerView;
        refreshLayout = bind.refreshLayout;
        empty = bind.empty;
//        refreshLayout.setEnableRefresh(true);
//        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        rv.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        adapter = getAdapter();
        DividerItemDecoration decoration = new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
        decoration.setDrawable(ContextCompat.getDrawable(getContext(),R.drawable.list_divider));
        rv.addItemDecoration(decoration);
        rv.setAdapter(adapter);
//        adapter.notifyDataSetChanged();

        return bind.getRoot();
    }

    protected abstract void afterViewCreated();

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Type[] arguments = type.getActualTypeArguments();
        if(arguments.length>1){
            Type argument=arguments[1];
            Class modelClaz=((Class)argument).asSubclass(AbsViewModel.class);
            mViewModel = (M) ViewModelProviders.of(this).get(modelClaz);
            mViewModel.getLiveData().observe(getViewLifecycleOwner(), new Observer<PagedList<T>>() {
                @Override
                public void onChanged(PagedList<T> pagedList) {
                    adapter.submitList(pagedList);
                }
            });
            mViewModel.getBoundaryLiveData().observe(getActivity(), new Observer<Boolean>() {
                @Override
                public void onChanged(Boolean aBoolean) {
                    finishRefresh(aBoolean);
                }
            });
        }
        afterViewCreated();
    }

    public void submitList(PagedList<T> pagedList){
        if(pagedList.size()>0){
            adapter.submitList(pagedList);
        }
        finishRefresh(pagedList.size()>0);
    }


    public void finishRefresh(boolean hasData){
        PagedList<T> currentList = adapter.getCurrentList();
        hasData=hasData||currentList !=null&&currentList.size()>0;
        RefreshState state = refreshLayout.getState();
        if(state.isFooter&&state.isOpening){
            refreshLayout.finishLoadMore();
        }else if(state.isHeader&&state.isOpening){
            refreshLayout.finishRefresh();
        }
        if(hasData){
            empty.setVisibility(View.GONE);
        }else {
            empty.setVisibility(View.VISIBLE);
        }
    }

    public abstract PagedListAdapter getAdapter();


}
